package com.hrm.controller;

import com.hrm.pojo.DTO.DocumentPageDTO;
import com.hrm.pojo.DTO.DocumentUpdateDTO;
import com.hrm.pojo.Document;
import com.hrm.service.DocumentService;
import com.hrm.utils.entity.PageResult;
import com.hrm.utils.entity.Result;
import com.hrm.utils.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/document")
@Api(tags = "文档相关接口")
public class DocumentController {
    @Autowired
    private DocumentService documentService;


    /**
     * 分页查询
     * @param documentPageDTO
     * @return
     * @throws Exception
     */
    @GetMapping("/page")
    @ApiOperation("文档列表")
    public Result showDocList(DocumentPageDTO documentPageDTO)throws Exception{
        PageResult<Document> result=documentService.selectByTitle(documentPageDTO);
        return new Result(result!=null, result!=null ? StatusCode.SUCCESS : StatusCode.ERROR,"操作成功",result);
    }

    /**
     * 删除文档
     * @param id
     * @return
     * @throws Exception
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除文档")
    public Result deleteDoc(@PathVariable Integer id)throws Exception{
        boolean flag=documentService.deleteById(id);
        return new Result(flag, flag ?StatusCode.SUCCESS :StatusCode.ERROR,"操作成功");
    }

    /**
     * 修改文档
     * @param documentUpdateDTO
     * @return
     * @throws Exception
     */
    @PutMapping("/update")
    @ApiOperation("修改文档")
    public Result updateDoc(@RequestBody DocumentUpdateDTO documentUpdateDTO)throws Exception{
        boolean flag=documentService.updateDoc(documentUpdateDTO);
        return new Result(flag, flag ?StatusCode.SUCCESS :StatusCode.ERROR,"操作成功");
    }

    /**
     * 增加文档
     * @param document
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ApiOperation("增加文档")
    public Result addDoc(@RequestBody Document document)throws Exception{
        boolean flag= documentService.addDoc(document);
        return new Result(flag, flag ?StatusCode.SUCCESS :StatusCode.ERROR,"操作成功");
    }


    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result fileUploads(@RequestParam("file") MultipartFile file) throws IOException {
        // 设置固定的日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //文件上传路径
        String filePath = "d:/uploads/";
        // 得到格式化后的日期
        String format = sdf.format(new Date());
        // 获取上传的文件名称
        String fileName = file.getOriginalFilename();
        // 时间 和 日期拼接
        String newFileName = format + "_" + fileName;
        // 得到文件保存的位置以及新文件名
        File dest = new File(filePath + newFileName);
        try {
            file.transferTo(dest);
            System.out.println("上传成功，当前上传的文件保存在"+ filePath + newFileName);
            return new Result(true,StatusCode.SUCCESS,"操作成功",dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,StatusCode.ERROR,"操作成功");
    }
}
